#include "test_ntp.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
static void test_ntp_cb(cm_ntp_event_e event, void *event_param, void *cb_param)
{
    if (CM_NTP_EVENT_SYNC_OK == event)
    {
        cm_demo_printf("[NTP][%s] get net time %s\n", (char *)cb_param, event_param); 
    }
    else if (CM_NTP_EVENT_SETTIME_FAIL == event)
    {
        cm_demo_printf("[NTP][%s] get net time %s OK, but set time fail\n", (char *)cb_param, event_param); 
    }
	else
	{
		cm_demo_printf("[NTP][%s] error\n", (char *)cb_param); 
	}
}


void cm_test_ntp_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("CFG",cmd[2],strlen("CFG")))
    {
        uint16_t port = atoi(cmd[4]);
		uint32_t timeout = 3000;
		uint32_t dns_priority = atoi(cmd[5]);
		bool set_rtc = atoi(cmd[6]);
		
    	cm_demo_printf("cm_ntp_set_cfg ret:%d\n",cm_ntp_set_cfg(CM_NTP_CFG_SERVER, cmd[3])); 
		cm_demo_printf("cm_ntp_set_cfg ret:%d\n",cm_ntp_set_cfg(CM_NTP_CFG_PORT, &port));
		cm_demo_printf("cm_ntp_set_cfg ret:%d\n",cm_ntp_set_cfg(CM_NTP_CFG_TIMEOUT, &timeout));
		cm_demo_printf("cm_ntp_set_cfg ret:%d\n",cm_ntp_set_cfg(CM_NTP_CFG_DNS, &dns_priority));
		cm_demo_printf("cm_ntp_set_cfg ret:%d\n",cm_ntp_set_cfg(CM_NTP_CFG_SET_RTC, &set_rtc));
		cm_demo_printf("cm_ntp_set_cfg ret:%d\n",cm_ntp_set_cfg(CM_NTP_CFG_CB, test_ntp_cb));
        int32_t ntp_set_cfg = cm_ntp_set_cfg(CM_NTP_CFG_CB_PARAM, "GETNETTIME");
		cm_demo_printf("cm_ntp_set_cfg ret:%d\n",ntp_set_cfg);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ntp_set_cfg,cm_ntp_set_cfg);
    }else if(0 == strncmp("SYNC",cmd[2],strlen("SYNC")))
    {
        int32_t ntp_sync = cm_ntp_sync();
        cm_demo_printf("cm_ntp_sync ret:%d\n",ntp_sync);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ntp_sync,cm_ntp_sync);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}